﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETL.Business.Handlers.ProjectCategoryHandler;
using ETL.Business.Handlers.ProjectHandlers;
using ETL.Business.Handlers.ProjectTaskHandler;
using ETL.Business.Handlers.ProjectTimeHandler;
using ETL.Business.Handlers.SessionHandler;
using ETL.Business.Models;
using ETL.Common;
using ETL.Common.Logging;

namespace ETL.Business.Helpers
{
    public class GetLastTask
    {
        public GetLastTask()
        {
        }

        public virtual void Execute()
        {
            try
            {
                // specity user Id
                string userId = (new GetSessionData()).Execute(Constants.SessionVars.UserId);

                ProjectTimeModel lastPrjTime = (new GetLastestProjectTime()).Execute(userId);
                if(lastPrjTime == null)
                {
                    // last project id
                    (new SetSessionData()).Execute(Constants.SessionVars.LastProjectId, string.Empty);
                    // last project name
                    (new SetSessionData()).Execute(Constants.SessionVars.LastProjectName, string.Empty);
                    // last cat id
                    (new SetSessionData()).Execute(Constants.SessionVars.LastCategoryId, string.Empty);
                    // last cat description
                    (new SetSessionData()).Execute(Constants.SessionVars.LastCategoryName, string.Empty);
                    // last task id
                    (new SetSessionData()).Execute(Constants.SessionVars.LastTaskId, string.Empty);
                    // last task name
                    (new SetSessionData()).Execute(Constants.SessionVars.LastTaskName, string.Empty);

                }
                else
                {
                    ProjectModel lastPrj = (new GetProjectById()).Execute(lastPrjTime.ProjectId);
                    ProjectCategoryModel lastCat = (new GetCategoryById()).Execute(lastPrjTime.ProjectId, lastPrjTime.CategoryId);
                    ProjectTaskModel lastTask = (new GetLastestProjectTask()).Execute(userId);

                    // set session vars
                    string lastActivity = string.Empty;
                    if (lastPrj != null)
                    {
                        // last project id
                        (new SetSessionData()).Execute(Constants.SessionVars.LastProjectId, lastPrj.ProjectId.ToString());
                        // last project name
                        (new SetSessionData()).Execute(Constants.SessionVars.LastProjectName, lastPrj.ProjectDescription);

                        // build string last activity
                        lastActivity += lastPrj.ProjectName;
                    }
                    else
                    {
                        // last project id
                        (new SetSessionData()).Execute(Constants.SessionVars.LastProjectId, string.Empty);
                        // last project name
                        (new SetSessionData()).Execute(Constants.SessionVars.LastProjectName, string.Empty);
                    }
                    if (lastCat != null)
                    {
                        // last cat id
                        (new SetSessionData()).Execute(Constants.SessionVars.LastCategoryId, lastCat.CategoryId.ToString());
                        // last cat description
                        (new SetSessionData()).Execute(Constants.SessionVars.LastCategoryName, lastCat.Description);
                        // build string last activity
                        lastActivity += " / " + lastCat.Description;
                    }
                    if (lastTask != null)
                    {
                        // last task id
                        (new SetSessionData()).Execute(Constants.SessionVars.LastTaskId, lastTask.TaskId.ToString());
                        // last task name
                        (new SetSessionData()).Execute(Constants.SessionVars.LastTaskName, lastTask.Description);
                        // build string last activity
                        lastActivity += " / " + lastTask.Description;
                    }

                    // last activity
                    (new SetSessionData()).Execute(Constants.SessionVars.LastActivity, lastActivity);
                }
               
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                throw;
            }

        }
    }
}
